<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');
// load base class if needed
require_once( APPPATH . 'controllers/base/OperatorBase.php' );

class aktifitas extends ApplicationBase {

    // constructor
    public function __construct() {
        parent::__construct();
        // load model
        $this->load->model('m_aktifitas');
        $this->load->model('m_tahunajar');
        $this->load->model('m_silabus');
        $this->load->model('m_dosen');
        //--
        $this->smarty->assign('aktif', $this->m_aktifitas);
        // load library
        $this->load->library('tnotification');
        $this->load->library('pagination');
    }

    // halaman list data
    public function index() {
        // set page rules
        $this->_set_page_rule("R");
        // set template content
        $this->smarty->assign("template_content", "monitoring/aktifitas/index.html");
        // tahun
        $rs_tahun = $this->m_silabus->get_tahun_ajaran();
        $rs_tahun = !empty($rs_tahun) ? $rs_tahun : array("0" => array("tahun" => date('Y')));
        $this->smarty->assign('rs_tahun', $rs_tahun);
        // semester
        $sms = array(1, 2, 3, 4, 5, 6, 7, 8);
        $this->smarty->assign('rs_sms', $sms);
        // get ta
        $rs_ta = $this->m_tahunajar->get_detail_data_tahunajar();
        // session
        $search = $this->tsession->userdata('search_aktifitas');
        $search['semester'] = !empty($search['semester']) ? $search['semester'] : $rs_ta['semester'];
        $search['tahun'] = !empty($search['tahun']) ? $search['tahun'] : $rs_ta['tahun'];
        $this->smarty->assign('search', $search);
        // params
        $tahun = $search['tahun'];
        $semester = $search['semester'];
        $nama_lengkap = !empty($search['nama_lengkap']) ? "%" . $search['nama_lengkap'] . "%" : "%";
        //--
        $params = array($tahun, $semester, $nama_lengkap);
        /* start of pagination --------------------- */
        // pagination
        $config['base_url'] = site_url("monitoring/aktifitas/index/");
        $config['total_rows'] = $this->m_aktifitas->get_total_dosen($params);
        $config['uri_segment'] = 4;
        $config['per_page'] = 20;
        $this->pagination->initialize($config);
        $pagination['data'] = $this->pagination->create_links();
        // pagination attribute
        $start = $this->uri->segment(4, 0) + 1;
        $end = $this->uri->segment(4, 0) + $config['per_page'];
        $end = (($end > $config['total_rows']) ? $config['total_rows'] : $end);
        $pagination['start'] = ($config['total_rows'] == 0) ? 0 : $start;
        $pagination['end'] = $end;
        $pagination['total'] = $config['total_rows'];
        // pagination assign value
        $this->smarty->assign("pagination", $pagination);
        $this->smarty->assign("no", $start);
        /* end of pagination ---------------------- */
        // get data
        $params = array($tahun, $semester, $nama_lengkap, ($start - 1), $config['per_page']);
        $result = $this->m_aktifitas->get_data_dosen($params);
        $this->smarty->assign('rs_id', $result);
        // notification
        $this->tnotification->display_notification();
        $this->tnotification->display_last_field();
        // output
        parent::display();
    }

    // proses search
    public function search_process() {
        // set page rules
        $this->_set_page_rule("R");
        //--
        if ($this->input->post('save') == 'Cari') {
            // params
            $params = array(
                "tahun" => $this->input->post('tahun'),
                "semester" => $this->input->post('semester'),
                "nama_lengkap" => $this->input->post('nama_lengkap')
            );
            // set session
            $this->tsession->set_userdata('search_aktifitas', $params);
        } else {
            // unset session
            $this->tsession->unset_userdata('search_aktifitas');
        }
        // redirect
        redirect('monitoring/aktifitas');
    }

    // halaman detail aktifitas dosen
    public function detail($tahun = "", $semester = "", $dosen_kd = "") {
        // set page rules
        $this->_set_page_rule("R");
        // set template content
        $this->smarty->assign("template_content", "monitoring/aktifitas/detail.html");
        // tahun
        $this->smarty->assign('tahun', $tahun);
        // semester
        $this->smarty->assign('semester', $semester);
        // dosen
        $this->smarty->assign('dosen_kd', $dosen_kd);
        // data dosen
        $rs_dosen = $this->m_dosen->get_detail_data_dosen($dosen_kd);
        $this->smarty->assign('rs_dosen', $rs_dosen);
        // data makul
        $rs_makul = $this->m_aktifitas->get_makul_diampu(array($tahun, $semester, $dosen_kd));
        $this->smarty->assign('rs_makul', $rs_makul);
        // data session
        $search = $this->tsession->userdata('search_aktifitas_detail');
        $this->smarty->assign('search', $search);
        // data
        $makul_kd = !empty($search['makul_kd']) ? $search['makul_kd'] : "%";
        $params = array($tahun, $semester, $dosen_kd, $makul_kd);
        $rs_id = $this->m_aktifitas->get_data_aktifitas_dosen($params);
        $this->smarty->assign('rs_id', $rs_id);
        // notification
        $this->tnotification->display_notification();
        $this->tnotification->display_last_field();
        // output
        parent::display();
    }

    // proses search detail
    public function search_detail() {
        // set page rules
        $this->_set_page_rule("R");
        //--
        $tahun = $this->input->post('tahun');
        $semester = $this->input->post('semester');
        $dosen_kd = $this->input->post('dosen_kd');
        if ($this->input->post('save') == 'Cari') {
            // params
            $params = array(
                "makul_kd" => $this->input->post('makul_kd')
            );
            // set session
            $this->tsession->set_userdata('search_aktifitas_detail', $params);
        } else {
            // unset session
            $this->tsession->unset_userdata('search_aktifitas_detail');
        }
        // redirect
        redirect('monitoring/aktifitas/detail/' . $tahun . '/' . $semester . '/' . $dosen_kd);
    }

}